package com.saint.farmshop.Service.ServiceImpl;

import com.saint.farmshop.Entity.Address;
import com.saint.farmshop.Entity.User;
import com.saint.farmshop.Mapper.MapperImpl.UserMapperImpl;
import com.saint.farmshop.Service.UserService;
import com.saint.farmshop.Utils.Result;
import com.saint.farmshop.Utils.ResultCode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户服务实现类
 *
 * @author Saint
 * @date 2022/05/21
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    UserMapperImpl userMapper;

    @Override
    public Result userRegist(String openId) {
        if(userMapper.isRegisted(openId)){
            return Result.ok();
        }
        else{
            return Result.RCode(false, ResultCode.USER_ACCOUNT_ALREADY_EXIST);
        }
    }

    @Override
    public User userLogin(User user) {
        if(user.getOpenId()==null){
            return null;
        }
        else {
            if (!userMapper.isRegisted(user.getOpenId())) {
                userMapper.insertUser(user);
            }
            return userMapper.selectUserByOpenId(user.getOpenId());
        }
    }

    @Override
    public Result updateUser(User user) {
        if(user.getOpenId()==null){
            return Result.RCode(false,ResultCode.USER_ACCOUNT_NOT_EXIST);
        }
        else {
            userMapper.updateUser(user);
            return Result.ok();
        }
    }

    @Override
    public List<User> getList() {
        return userMapper.getList();
    }

    @Override
    public Result addAddress(Address address, String openId) {
        if(userMapper.addAddress(address,openId) == true){
            return Result.ok();
        }
        else{
            return Result.error();
        }
    }

}
